package edu.whu.rose.clock.ssql.graphindex;

import java.io.UnsupportedEncodingException;

import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;

import edu.whu.rose.clock.ssql.binding.GraphIndexItemBinding;
import edu.whu.rose.clock.ssql.binding.NewGraphIndexItemBinding;
import edu.whu.rose.clock.ssql.index.IndexDB;

public class GraphIndexDB extends IndexDB {

	public GraphIndexDB(Environment dbEnv) {
		super(dbEnv, "keyword_subgraphs_list");
	}
	
	public GraphIndexDB(Environment dbEnv, String dbName) {
		super(dbEnv, dbName);
	}
	
	public GraphIndexItem search(String keyword) {
		GraphIndexItem result = null;
		keyword = keyword.toLowerCase();
		try {
			DatabaseEntry list = this.get(new DatabaseEntry(keyword.getBytes("UTF-8")));
			if (list != null) {
				GraphIndexItemBinding giib = new GraphIndexItemBinding();
				result = (GraphIndexItem)giib.entryToObject(list);
			}
		}
		catch (DatabaseException ex) {
			ex.printStackTrace();
		}
		catch (UnsupportedEncodingException ex) {
			ex.printStackTrace();
		}
		catch (NumberFormatException ex) {
			ex.printStackTrace();
		}
		return result;
	}
	
	public NewGraphIndexItem newSearch(String keyword) {
		NewGraphIndexItem result = null;
		keyword = keyword.toLowerCase();
		try {
			DatabaseEntry list = this.get(new DatabaseEntry(keyword.getBytes("UTF-8")));
			if (list != null) {
				NewGraphIndexItemBinding giib = new NewGraphIndexItemBinding();
				result = (NewGraphIndexItem)giib.entryToObject(list);
			}
		}
		catch (DatabaseException ex) {
			ex.printStackTrace();
		}
		catch (UnsupportedEncodingException ex) {
			ex.printStackTrace();
		}
		catch (NumberFormatException ex) {
			ex.printStackTrace();
		}
		return result;
	}
	
//	public GraphIndexItem2 search2(String keyword) {
//		GraphIndexItem2 result = null;
//		keyword = keyword.toLowerCase();
//		try {
//			DatabaseEntry list = this.get(new DatabaseEntry(keyword.getBytes("UTF-8")));
//			if (list != null) {
//				GraphIndexItemBinding2 giib = new GraphIndexItemBinding2();
//				result = (GraphIndexItem2)giib.entryToObject(list);
//			}
//		}
//		catch (DatabaseException ex) {
//			ex.printStackTrace();
//		}
//		catch (UnsupportedEncodingException ex) {
//			ex.printStackTrace();
//		}
//		catch (NumberFormatException ex) {
//			ex.printStackTrace();
//		}
//		return result;
//	}

}
